package com.bhs.sansonglogistics.ui.wallet;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.base.BaseBean;
import com.bhs.sansonglogistics.bean.CloseAccountBean;
import com.bhs.sansonglogistics.bean.RedEnvelopeRule;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.SatisfyAdapter;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 红包转余额
 */
public class RedEnvelopeTransferBalanceActivity extends BaseActivity implements NetCallBack, View.OnClickListener {

    private TextView mTvMoney;
    private RecyclerView mRvList;
    private Button mBtnSubmit;
    private String balance;
    private SatisfyAdapter mAdapter;
    private List<CloseAccountBean> mList = new ArrayList<>();

    @Override
    public int initLayout() {
        return R.layout.activity_red_envelope_transfer_balance;
    }

    @Override
    public void initView() {
        balance = getIntent().getStringExtra("balance");
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("红包转余额");
        mTvMoney = findViewById(R.id.tv_money);
        mRvList = findViewById(R.id.rv_list);
        mBtnSubmit = findViewById(R.id.btn_submit);
        mBtnSubmit.setOnClickListener(this);
    }

    @Override
    public void initData() {
        mTvMoney.setText(balance);
        networkRequest(netApi.get_redpack_config("logistics"), this, 777);
        mList.add(new CloseAccountBean("满足金额", "", false));
        mList.add(new CloseAccountBean("完结订单", "", false));
        mAdapter = new SatisfyAdapter(mList);
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mContext));
    }

    @Override
    public void onClick(View view) {
        networkRequest(netApi.apply_zhuanyuee(balance), this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 777) {
            RedEnvelopeRule infoBean = new Gson().fromJson(json, RedEnvelopeRule.class);
            if (infoBean.isStatus()) {
                mList.clear();
                boolean satisfyMoney = Double.parseDouble(balance) >= Double.parseDouble(infoBean.getData().getMinMoney());
                boolean satisfyOrder = infoBean.getData().getOrderNumber() >= infoBean.getData().getMinOrder();
                mList.add(new CloseAccountBean("满足金额", String.format("%s/%s", balance, infoBean.getData().getMinMoney()), satisfyMoney));
                mList.add(new CloseAccountBean("完结订单", String.format("%s/%s",
                        infoBean.getData().getOrderNumber(), infoBean.getData().getMinOrder()), satisfyOrder));
                mBtnSubmit.setEnabled(satisfyOrder && satisfyMoney);
                mBtnSubmit.setBackgroundColor(satisfyOrder && satisfyMoney ? Color.parseColor("#485EF4") : Color.parseColor("#7F485EF4"));
                mAdapter.setNewData(mList);
            }
        } else {
            BaseBean bean = new Gson().fromJson(json, BaseBean.class);
            ToastUtil.show(bean.getMsg());
            if (bean.isStatus()) {
                startActivity(new Intent(mActivity, SuccessfullyAddedActivity.class));
                finish();
            }
        }


    }

    @Override
    public void onFailure(int requestCode) {

    }
}